package com.qk.management.controller;

import com.qk.common.Result;
import com.qk.management.anno.LogOperation;
import com.qk.management.util.AliyunOSSOperators;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author 16778
 * @date 2025/10/8 15:10
 * @description
 */
@Slf4j
@RestController
public class FileHandleController {
    @Autowired
    private AliyunOSSOperators oss;

//    @PostMapping("/upload")
    public String upload(MultipartFile file, String username, Integer age) throws IOException {
        log.info("用户名:{},年龄:{},上传文件:{}",username,age,file);
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getName());
        System.out.println(file.getContentType());
        file.transferTo(new File("D://"+file.getOriginalFilename()));
        return "上传成功";
    }

    @LogOperation
    @PostMapping("/upload")
    public Result upload(MultipartFile image) {
        log.info("上传文件:{}",image);
        try {
            String name = UUID.randomUUID()+image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."));
            String url = oss.upload(image.getBytes(), name);
            return Result.success(url);
        } catch (Exception e) {
            log.error("文件上传失败:{}",e.getMessage());
        }
        return null;
    }
}
